"""
conanfile.py for cucon
以下指令用于屏蔽无用警告信息
"""

# pyright: reportGeneralTypeIssues=false
# pyright: reportUnknownMemberType=false
# pyright: reportUnknownVariableType=false
# pyright: reportMissingModuleSource=false
# pyright: reportAttributeAccessIssue=false
# pyright: reportMissingImports=false
# pyright: reportOptionalCall=false
# pyright: reportOptionalMemberAccess=false
import pathlib as ptl

from conan import ConanFile
from conan.tools import files as cnn_files
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout


class CompressorRecipe(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    generators = "CMakeDeps"

    def requirements(self):
        # self.requires("zlib/1.3.1")
        self.requires("spdlog/1.15.3")
        self.requires("fmt/11.2.0")

        # ==== 根据平台选择依赖; packages
        if self.settings.os == "Windows":
            # self.requires("base64/0.4.0")
            self.requires("expat/2.7.3")
            pass
        elif self.settings.os == "Linux":
            # 添加 Linux 特定的依赖
            # self.requires("openssl/3.2.1")
            # self.requires("libuuid/1.47.0")
            self.requires("expat/2.7.3")
            pass

    def build_requirements(self):
        # 根据平台选择构建依赖的工具; build tools
        if self.settings.os != "Windows":
            # Linux 平台需要的构建工具
            self.tool_requires("cmake/4.1.1")
            self.tool_requires("ninja/1.13.1")
            pass
        elif self.settings.os == "Windows":
            # Windows 平台可以添加特定的构建工具
            pass

    def layout(self):
        if self.settings.os == "Linux":
            # Linux 平台使用统一的构建目录
            cmake_layout(self, build_folder="build")
        elif self.settings.os == "Windows":
            # 检查当前使用的 profile 是否是 ninjaRelease
            if self.settings.get_safe("compiler") == "msvc" and self.conf.get("tools.cmake.cmaketoolchain:generator") == "Ninja":
                # cmake_layout(self, build_folder="build-ninja")
                cmake_layout(self, build_folder="build")
            else:
                # 为 MSVC 设置特殊的目录
                cmake_layout(self, build_folder="build-msvc")

    def generate(self):
        ##--- 设定编译的线程数为8
        self.conf_info.define("tools.build:jobs", 8)
        tc = CMakeToolchain(self, generator="Ninja")
        tc.generate()

        ## 拷贝 .npz 文件到程序的 binary 目录；当前通过命令行传递参数, 无需拷贝
        if False:
            self._copy_npz_files()

        # 把依赖包里的 .so 全部拷到 build/ 目录
        if True:
            if not self.build_folder:
                raise ValueError("build_folder is None, cannot copy 3rd dlls to runtime directory")
            # self._copy_3rd_dlls(ptl.Path(self.build_folder) / "runtime")
            self._copy_3rd_dlls(ptl.Path(self.build_folder))

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    # ========================= 让自己的代码编译为 Release 版本
    # def configure(self):
    #   self.settings.build_type = "Release"

    ##==================================== 拷贝 3rd 动态库 到 build/Debug/runtime 目录
    def _copy_3rd_dlls(self, dst: ptl.Path):
        if self.build_folder is None:
            self.output.warning("build_folder is None")
            return
        # dst = ptl.Path(self.build_folder) / "runtime"
        for dep in self.dependencies.values():
            if dep.cpp_info.bindirs:
                self.output.info(f"[ymy bin dir]{dep.cpp_info.bindirs}")
                cnn_files.copy(self, "*.dll", dep.cpp_info.bindirs[0], dst=dst, keep_path=False)
            for libdir in dep.cpp_info.libdirs:
                self.output.info(f"[ymy lib dir]{dep.cpp_info.libdirs}")
                cnn_files.copy(self, "*.so*", libdir, dst=dst, keep_path=False)
                cnn_files.copy(self, "*.dylib", libdir, dst=dst, keep_path=False)

    ##==================================== 拷贝 .npz 文件到程序的 binary 目录
    def _copy_npz_files(self):
        if self.build_folder is None:
            self.output.warning("build_folder is None")
            return
        if self.source_folder is None:
            self.output.warning("source_folder is None")
            return

        # 源目录：test_io目录下的.npz文件
        src_dir = ptl.Path(self.source_folder) / "test_io"
        if not src_dir.exists():
            self.output.warning(f"test io dir not exists: {src_dir}")
            return

        # 目标目录：程序的binary目录
        dst_dir = ptl.Path(self.build_folder) / "test_io"

        # 拷贝所有.npz文件
        self.output.info(f"拷贝 .npz 文件从 {src_dir} 到 {dst_dir}")
        try:
            cnn_files.copy(self, "*.npz", src=src_dir, dst=dst_dir, keep_path=False)
            self.output.info("成功拷贝 .npz 文件")
        except Exception as e:
            self.output.error(f"拷贝 .npz 文件失败: {e}")
